package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DUserLogisticsService;
import cn.huiyunche.base.service.mappers.DWaybillLogisticsMapper;
import cn.huiyunche.base.service.model.DWaybillLogistics;
import cn.huiyunche.base.service.model.DWaybillLogisticsExample;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.SupplierLogisticsVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @FileName: cn.huiyunche.service.base.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/12/8 下午6:08
 */
@Service
public class DUserLogisticsServiceImpl implements DUserLogisticsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DUserLogisticsServiceImpl.class);

    @Autowired
    private DWaybillLogisticsMapper dWaybillLogisticsMapper;

    @Override
    public Map<String, Object> getListByDWId(PageVo page, Map<String, Object> paramsMap) {
        LOGGER.info("getListByDWId params : {}, {}", page, paramsMap);

        //分页参数
        if (null != page) {
            String orderByClause = StringUtils.isNotBlank(page.getOrder()) == true ? page.getOrder()
                    : " dw.id";
            paramsMap.put("limitStart", page.getStartIndex());
            paramsMap.put("limitEnd", page.getPageSize());
            paramsMap.put("orderByClause", orderByClause);

        }

        DWaybillLogisticsExample example = new DWaybillLogisticsExample();
        example.createCriteria().andWaybillIdEqualTo((Long) paramsMap.get("dWaybillId"));

        paramsMap.clear();
        List<DWaybillLogistics> list = dWaybillLogisticsMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            List<SupplierLogisticsVo> vos = new ArrayList<>();
            list.forEach(d -> {
                SupplierLogisticsVo vo = new SupplierLogisticsVo();
                vo.setLocation("您的车辆当前位置：" + d.getCity());
                vo.setCreateTime(d.getCreateTime());
                vo.setLonglat(d.getLongitude() + "," + d.getLatitude());
                vos.add(vo);
            });

            paramsMap.put("list", vos);
        }

        if (null != page) {
            page.setTotalRecord(dWaybillLogisticsMapper.countByExample(example));
            paramsMap.put("page", page);
        }

        return paramsMap;
    }
}
